/**
 * Created by sunshine on 2017/4/28.
 */
$(function() {
    //pop tooltip
    $("[data-toggle='tooltip']").tooltip();
    
    //close window reset form data
    $('body').on('hidden.bs.modal', '.modal', function (y) {
        if (y.target === this) {
            $(this).removeData('bs.modal');
        }
    });

    $('body').on('shown.bs.modal', '.modal', function (y) {
        if (y.target === this) {
        	checkAll();
        }
    });
    
    // 表格复选框全选
    checkAll();
});

/**
 * 全选复选框
 */
var checkAll = function (){
	$("th input:checkbox").click(function() {
        var checkedStatus = this.checked;
        var checkbox = $(this).parents('.table').find('tbody tr td:first-child input:checkbox');
        checkbox.each(function() {
            this.checked = checkedStatus;
            if (checkedStatus == this.checked) {
                $(this).closest('.checker > span').removeClass('checked');
            }
            if (this.checked) {
                $(this).closest('.checker > span').addClass('checked');
            }
        });
    });
}

/**
 * show tip message
 * @param msg
 * @param className
 */
var messageBox = function (msg, className, glyphicon, hideFunc) {
    var dialog = bootbox.dialog({
        size: "300",
        className: "alert alert-"+className,
        message: '<p><i class="glyphicon glyphicon-'+glyphicon+'" style="font-size: 24px;"></i> '+ msg +'</p>', 
        closeButton: true
    });
    dialog.on('shown.bs.modal',function () {
        //on shown do something
    });
    dialog.on("hidden.bs.modal", function() {
        //on hidden do something
    	if(hideFunc && (typeof hideFunc == 'function')){
    		hideFunc();
    	}
    });
    // do something in the background
    setTimeout(function(){
        dialog.modal('hide');
        if(hideFunc && (typeof hideFunc == 'function')){
    		hideFunc();
    	}
    }, 3000);
}

/**
 * show alert
 * @param msg
 */
var alert = function (msg, callback) {
	bootbox.alert({
		title: "警告",
	    message: msg,
	    callback: callback
	});
}

/**
 * show confirm
 * @param msg
 * @param url
 */
var confirm = function (msg, callback) {
    bootbox.confirm({
        title: '警告',
        size: "300",
        message:  msg,
        buttons: {
        	cancel: {
                label: '<i class="glyphicon glyphicon-remove-sign"></i> 取消',
                className: 'btn-success'
            },
            confirm: {
                label: '<i class="glyphicon glyphicon-ok-sign"></i> 确定',
                className: 'btn-danger'
            }
        },
        callback: callback
    });
}

/**
 * ajax request
 * @param url
 * @param callback
 */
var ajaxReq = function (url, callback) {
    $.ajax({
        url : url,
        success : function(json) {
            if (json.success) {
            	messageBox(json.message,'success','ok-sign', callback);
            } else {
                messageBox(json.message,'error','remove-sign');
            }
        },
        error : function(e) {
        	console.log(e);
            messageBox("请求出错:"+e,'error','warning-sign'); 
        }
    });
}
